package co.crossoft.configuration;


import com.alibaba.dubbo.config.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by ervin on 2017/8/2.
 */
@Configuration
public class DubboBaseConfiguration {

    @Bean
    public ApplicationConfig applicationConfig(@Value("${dubbo.application.name}") String name) {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(name);
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registry(@Value("${dubbo.registry.address}") String address) {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setId("zookeeper");
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress(address);
        return registryConfig;
    }

    @Bean
    public ProtocolConfig dubboProtocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(30001);
        protocolConfig.setDispatcher("all");
        protocolConfig.setThreadpool("cached");
        protocolConfig.setThreads(30000);
        protocolConfig.setSerialization("kryo");
        return protocolConfig;
    }

   /* @Bean
    public MonitorConfig monitorConfig() {
        MonitorConfig mc = new MonitorConfig();
        mc.setProtocol("registry");
        return mc;
    }*/

    @Bean
    public ReferenceConfig referenceConfig() {
        ReferenceConfig rc = new ReferenceConfig();
        //rc.setMonitor(monitorConfig());
        return rc;
    }

    @Bean
    public ProviderConfig providerConfig(RegistryConfig registry) {
        ProviderConfig providerConfig = new ProviderConfig();
        //providerConfig.setMonitor(monitorConfig());
        providerConfig.setRegistry(registry);
        providerConfig.setOwner("Snow");
        providerConfig.setLoadbalance("random");
        providerConfig.setActives(0);
        providerConfig.setExecutes(30000);
        return providerConfig;
    }

}
